package controller

import (
	"admin/contrib/session"
	"admin/model"
	"common/helper"
	"encoding/json"
	"github.com/valyala/fasthttp"
)

type RateController struct{}

type rateForm_t struct {
	Id   int     `json:"id"`
	Ty   uint32  `json:"ty"`
	Rate float64 `json:"rate"`
	Min  float64 `json:"min"`
	Max  float64 `json:"max"`
}

func (that *RateController) List(ctx *fasthttp.RequestCtx) {

	data, err := model.RateListList()
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, data)
}

func (that *RateController) Update(ctx *fasthttp.RequestCtx) {

	param := rateForm_t{}
	body := ctx.PostBody()

	err := json.Unmarshal(body, &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	admin := session.AdminGet(ctx)

	err = model.RateListUpdate(admin["name"], param.Id, param.Ty, param.Rate, param.Min, param.Max)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}
